package com.ruoyi.datastatistics.controller;

import com.ruoyi.common.core.domain.Parameter;
import com.ruoyi.datastatistics.domain.AgegroupResult;
import com.ruoyi.datastatistics.service.BodycountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bodycount")
public class BodycountController {
    @Autowired
    private BodycountService bodycountService;

    // 人数统计-年龄组
    @PostMapping("/getAgegroupstatistics")
    public Map<String,Object> getAgegroupstatistics(@RequestBody Parameter parameter){

        Map<String,Object> datamap = new HashMap<>();
        Map<String,Object> totalmap = new HashMap<>();

        //List<AgegroupResult> Result = bodycountService.getAgegroupstatistics(parameter);
        Map<String,Object> data =  bodycountService.getAgegroupstatistics(parameter);
        List<AgegroupResult> Result = (List<AgegroupResult>) data.get("data");
        // 使用并行流计算（大数据量时更高效）
        double totalSum = Result.parallelStream()
                .mapToDouble(AgegroupResult::getTotalvalue)
                .sum();

        double manSum = Result.parallelStream()
                .mapToDouble(AgegroupResult::getManvalue)
                .sum();

        double womanSum = Result.parallelStream()
                .mapToDouble(AgegroupResult::getWomanvalue)
                .sum();
        totalmap.put("totalSum",totalSum);
        totalmap.put("manSum",manSum);
        totalmap.put("womanSum",womanSum);
        datamap.put("totalmap",totalmap);
        datamap.put("Result",Result);
        datamap.put("message",data.get("message"));
        return datamap;
    }

    // 人数统计-年龄段
    @PostMapping("/getAgebracketstatistics")
    public Map<String,Object> getAgebracketstatistics(@RequestBody Parameter parameter){
        return bodycountService.getAgebracketstatistics(parameter);
    }

    // 人数统计-城乡工作种类
    @PostMapping("/getUrbanandruralstatistics")
    public Map<String,Object> getUrbanandruralstatistics(@RequestBody Parameter parameter){
        return bodycountService.getUrbanandruralstatistics(parameter);
    }
    // 人数统计-月份
    @PostMapping("/getMonthstatistics")
    public Map<String,Object> getMonthstatistics(@RequestBody Parameter parameter){
        return bodycountService.getMonthstatistics(parameter);
    }
    // 人数统计-下一机构
    @PostMapping("/getNextdeptstatistics")
    public Map<String,Object> getNextdeptstatistics(@RequestBody Parameter parameter){
        return bodycountService.getNextdeptstatistics(parameter);
    }
}
